﻿using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using PPIDesktopClient.General;

//using GoogleEarthClasses.Global;

namespace PPIDesktopClient.General
{
    public class ImagesHelper
    {
        /// <summary>
        /// Resize a byte image
        /// </summary>
        /// <param name="imageFile">Source Image</param>
        /// <param name="targetSize">Width</param>
        /// <returns></returns>
        public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
        {
            Image original = null;
            Image imgPhoto = null;
            Bitmap bmPhoto = null;
            Graphics grPhoto = null;
            MemoryStream mm = null;
            try
            {
                original = Image.FromStream(new MemoryStream(imageFile));
                int targetH, targetW;
                if (original.Height > original.Width)
                {
                    targetH = targetSize;
                    targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
                }
                else
                {
                    targetW = targetSize;
                    targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
                }
                imgPhoto = Image.FromStream(new MemoryStream(imageFile));
                // Create a new blank canvas.  The resized image will be drawn on this canvas.
                bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
                // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
                mm = new MemoryStream();
                bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                return mm.GetBuffer();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                original.Dispose();
                imgPhoto.Dispose();
                bmPhoto.Dispose();
                grPhoto.Dispose();
                mm.Close();
                mm.Dispose();
            }
        }

        /// <summary>
        /// Crate a image from bytes, and save in the Image-Cache folders
        /// </summary>
        /// <param name="ImageBytes">source image</param>
        /// <param name="ImageName">ImageName</param>
        /// <returns></returns>
        public static string CreateImageFromBytes(byte[] ImageBytes, string pathName)
        {
            MemoryStream ms = null;
            Image Photo = null;

            if (!Directory.Exists(Constants.k_PathSaveImages))
                Directory.CreateDirectory(Constants.k_PathSaveImages);
            try
            {
                string DestinationPath = pathName;
                ms = new MemoryStream(ImageBytes, 0, ImageBytes.Length);
                Photo = Image.FromStream(ms);
                Photo.Save(DestinationPath);
                return DestinationPath;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ms.Close();
                Photo.Dispose();
            }


        }
    }
}
